package com.example.ymm.modules1.controller.hl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.hl.yw.YwApprove;
import com.example.ymm.modules1.entity.hl.yw.YwDept;
import com.example.ymm.modules1.entity.ms.DEmployee;
import com.example.ymm.modules1.entity.my.user.DbApprovalLog;
import com.example.ymm.modules1.query.DbApprovalLogQueryCriteria;
import com.example.ymm.modules1.query.hl.YwDeptQueryCriteria;
import com.example.ymm.modules1.query.hl.YwMoneyAffirmLogCountQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.*;
import com.example.ymm.modules1.service.dto.heng.kq.KqCreateDto;
import com.example.ymm.modules1.service.dto.ms.CountMoneyUserDto;
import com.example.ymm.modules1.service.hl.*;
import com.example.ymm.modules1.service.hl.kq.KqDakaLogService;
import com.example.ymm.modules1.service.user.DbApprovalLogService;
import com.example.ymm.modules1.service.xft.XftService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.RedisUtil;
import com.example.ymm.util.UtilValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/hl/heng/yw/approval")
@Api(tags = "hl 业务抽成审批接口")
@Slf4j
@CrossOrigin
public class YwApprovalController extends BaseController {

    @Autowired
    DbApprovalLogService dbApprovalLogService;
    @Autowired
    YwApproveService ywApproveService;
    @Autowired
    YwMoneyAffirmLogService ywMoneyAffirmLogService;
    @Autowired
    YwUserService ywUserService;
    @Autowired
    XftService xftService;
    @Autowired
    DknTrialItemService dknTrialItemService;
    @Autowired
    private YwDeptService ywDeptService;
    @Autowired
    RedisUtil redisUtil;
    @Value("${spring.profiles.active}")
    private String PD_MATCH;

    @Autowired
    KqDakaLogService kqDakaLogService;


    @ApiOperation(value = "业务员提成 已审批列表")
    @PostMapping("/ywApprovalList")
    public CommonResult ywApprovalList(DbApprovalLogQueryCriteria criteria, YmPageDto ymPage) {
        try {
            String userName = getUserName();
            String userRole = getUserRole();
            String userId = getUserId();

            boolean is_show_all=false;//是否拥有查看所有数据的权限
            if ("manage".equals(userRole)) {
                is_show_all=true;
            }
            if(userName.equals("杨柳")|| userName.equals("张爱君")||userName.equals("邵琪")||userName.equals("庄嫚艺")){
                is_show_all=true;
            }
            if (!is_show_all) {
                criteria.setAllUserIds(userId);
            }
            //查询是否经理
            if (!"manage".equals(userRole)) {
                YwDeptQueryCriteria ywDeptQueryCriteria = new YwDeptQueryCriteria();
                ywDeptQueryCriteria.setManageUserName(userName);
                List<YwDept> ywDepts = ywDeptService.queryAll(ywDeptQueryCriteria);
                if(UtilValidate.isNotEmpty(ywDepts)) {
                    YwDept ywDept = ywDepts.get(0);
                    List<String>  deptNoIn=new ArrayList<>();
                    deptNoIn.add(ywDept.getDeptNo());
                    System.out.println("---deptNoIn:"+deptNoIn);
//                criteria.setDeptNoIn(deptNoIn);
//                criteria.setAllUserIds(null);
                }
            }
            TableList<DbApprovalLog> list = dbApprovalLogService.list(criteria, ymPage);
            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "表部汇总列表")
    @PostMapping("/ywDeptTotalList")
    public CommonResult ywDeptTotalList(DbApprovalLogQueryCriteria criteria, YmPageDto ymPage) {
        try {
            List<String> salesmanDeptNos=new ArrayList<>();
            /**
             * HL02-2  迪卡侬营销部
             * HL02-3  OEM营销部
             */
            String userName = getUserName();
            String userRole = getUserRole();

            DEmployee userInfo = ywUserService.getUserInfoToSlave2(userName);
            if(UtilValidate.isNotEmpty(userInfo)){
                String dname = userInfo.getDname();
                //迪卡侬和OEM营销部
                if(dname.equals("迪卡侬营销部")||dname.equals("OEM营销部")){
                    salesmanDeptNos.add("HL02-2");
                    salesmanDeptNos.add("HL02-3");
                    salesmanDeptNos.add("HL08-0-7");//东莞恒丽

                }else{
                    salesmanDeptNos.add(userInfo.getDtid());
                }
            }
            if(UtilValidate.isNotEmpty(criteria.getMonth())){
                String month = criteria.getMonth();
                String start=month+"-01 00:00:00";
                String end = DateKit.getNextMonthStartDate(start);
                criteria.setStartTime(start);
                criteria.setEndTime(end);
            }
            if(UtilValidate.isNotEmpty(criteria.getDeptNo())){
                String[] split = criteria.getDeptNo().split(",");
                for(String deptNo:split){
                    salesmanDeptNos.add(deptNo);
                }
            }
            if(UtilValidate.isEmpty(criteria.getStatus())){
                criteria.setStatus(2);
            }
            criteria.setDeptNoIn(salesmanDeptNos);

            TableList<DbApprovalLog> list = dbApprovalLogService.ywDeptTotalList(criteria, ymPage);
            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 审批详情")
    @PostMapping("/getCountDeptTotalLog_biao")
    public CommonResult getCountDeptTotalLog_biao(YwMoneyAffirmLogCountQueryCriteria criteria) {
            try {
                List<String> salesmanDeptNos=new ArrayList<>();

                String userName = getUserName();
                String userRole = getUserRole();
                if (!"manage".equals(userRole)) {
                    DEmployee userInfo = ywUserService.getUserInfoToSlave2(userName);
                    if(UtilValidate.isNotEmpty(userInfo)){
                        String dname = userInfo.getDname();
                        //迪卡侬和OEM营销部
                        if(dname.equals("迪卡侬营销部")||dname.equals("OEM营销部")){
                            salesmanDeptNos.add("HL02-2");
                            salesmanDeptNos.add("HL02-3");
                            salesmanDeptNos.add("HL08-0-7");//东莞恒丽
                        }else{
                            salesmanDeptNos.add(userInfo.getDtid());
                        }
                    }
                }
                if(UtilValidate.isNotEmpty(criteria.getMonth())){
                    String month = criteria.getMonth();
                    String start=month+"-01 00:00:00";
                    String end = DateKit.getNextMonthStartDate(start);
                    criteria.setStartTime(start);
                    criteria.setEndTime(end);
                }
                if(UtilValidate.isNotEmpty(criteria.getDeptNo())){
                    String[] split = criteria.getDeptNo().split(",");
                    for(String deptNo:split){
                        salesmanDeptNos.add(deptNo);
                    }
                }

                criteria.setSalesmanDeptNoIn(salesmanDeptNos);
                List<CountMoneyUserDto> list = ywMoneyAffirmLogService.getCountDeptTotalLog_biao(criteria);

                return CommonResult.success(list);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return CommonResult.error(e.getMessage());
            }
        }



    @ApiOperation(value = "业务员提成 审批详情")
    @PostMapping("/ywApprovalDetailByApproveNo")
    public CommonResult ywApprovalDetailByApproveNo(String approveNo) {
        try {
            DbApprovalLog approveNo1 = dbApprovalLogService.getApproveNo(approveNo);
            YwApprove byApproveNo = ywApproveService.findByApproveNo(approveNo);
            if(UtilValidate.isNotEmpty(byApproveNo)){
                approveNo1.setApprove(byApproveNo);
            }
            return CommonResult.success(approveNo1);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业务员提成 审批详情")
    @PostMapping("/ywApprovalDetail")
    public CommonResult ywApprovalDetail(String approvalLogId) {
        try {
            DbApprovalLog approvalLog = dbApprovalLogService.getById(approvalLogId);
            return CommonResult.success(approvalLog);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

//    @ApiOperation(value = "业务员提成 管理员强制审批")
//    @PostMapping("/ywManageDelete")
//    public CommonResult ywManageDelete(String approveNo) {
//        try {
//            ywApproveService.approveCancel(approveNo);
//            return CommonResult.success();
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            return CommonResult.error(e.getMessage());
//        }
//    }


    @ApiOperation(value = "业务员提成 发起前查看审批人")
    @PostMapping("/ywApprovalCreateView")
    public CommonResult ywApprovalCreateView(@RequestParam String approvalName,String title) {
        try {
            // "业务员提成审批"
            ApprovalCreateViewDto dto = ywApproveService.ywApprovalCreateView(approvalName,title);
            return CommonResult.success(dto);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 查看审批进度")
    @PostMapping("/ywApprovalLogView")
    public CommonResult ywApprovalLogView(String approveNo) {
        try {
            ApprovalCreateViewDto dto = ywApproveService.ywApprovalLogView(approveNo);
            return CommonResult.success(dto);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 审批提起")
    @PostMapping("/ywApprovalSubmit")
    public CommonResult ywApprovalSubmit(@RequestBody YwApproveCreateDto dto) {
        try {
            ApproveRespond approveRespond = ywApproveService.ywApprovalSubmit(dto);
            return CommonResult.success(approveRespond);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 文件 审批提起")
    @PostMapping("/ywApprovalFileSubmit")
    public CommonResult ywApprovalFileSubmit(YwApproveCreateK3Dto dto) {
        try {
            ApproveRespond approveRespond = ywApproveService.ywApprovalFileSubmit(dto);
            return CommonResult.success(approveRespond);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 审批通过")
    @PostMapping("/ywApprovalPass")
    public CommonResult ywApprovalPass(YwApprovaPassDto dto) {
        try {
            ywApproveService.ywApprovalPass(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 审批驳回")
    @PostMapping("/ywApprovalBack")
    public CommonResult ywApprovalBack(YwApprovaPassDto dto) {
        try {
            ywApproveService.ywApprovalBack(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业务员提成 申请人撤回")
    @PostMapping("/ywApprovalCancel")
    public CommonResult ywApprovalCancel(@ApiParam(value = "数据主键") @RequestParam(required = true) String approveNo) {
        try {
            ywApproveService.ywApprovalCancel(approveNo);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 申请人撤回")
    @PostMapping("/ywApprovalRemove")
    public CommonResult ywApprovalRemove(@ApiParam(value = "数据主键") @RequestParam(required = true) String approveNo) {
        try {
            ywApproveService.ywApprovalRemove(approveNo);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "业务员提成 按钮显示")
    @PostMapping("/ywApprovalShow")
    public CommonResult ywApprovalShow(@ApiParam(value = "数据主键") @RequestParam(required = true) String approveNo) {
        try {
            ApprovalButtonShowDto dto = dbApprovalLogService.approveButtonShow(approveNo);
            return CommonResult.success(dto);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "审批单号提成统计")
    @PostMapping("/countApproverNoTotal")
    public CommonResult countApproverNoTotal(@ApiParam(value = "数据主键") @RequestParam(required = true) String approveNo) {
        try {
            YwMoneyAffirmLogCountQueryCriteria criteria=new YwMoneyAffirmLogCountQueryCriteria();
            criteria.setApproverNo(approveNo);
            List<CountMoneyUserDto> dtos = ywMoneyAffirmLogService.countApproverNoTotal(criteria);
            return CommonResult.success(dtos);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "添加审批评论")
    @PostMapping("/addYwComment")
    public CommonResult addYwComment(YwApprovaPassDto dto) {
        try {
            if(UtilValidate.isEmpty(dto.getApproveNo())){
                return CommonResult.error("审批编号不能为空！");
            }
            ywApproveService.addYwComment(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }






    @ApiOperation(value = "配送费 审批提起")
    @PostMapping("/ywPostageSubmit")
    public CommonResult ywPostageSubmit(@RequestBody YwPostageCreateDto dto) {
        try {
            ApproveRespond approveRespond = ywApproveService.ywPostageSubmit(dto);
            return CommonResult.success(approveRespond);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "配送费 审批通过")
    @PostMapping("/ywPostagePass")
    public CommonResult ywPostagePass(YwApprovaPassDto dto) {
        try {
            ywApproveService.commonPass(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "配送费 审批驳回")
    @PostMapping("/ywPostageBack")
    public CommonResult ywPostageBack(YwApprovaPassDto dto) {
        try {
            ywApproveService.commonBack(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "配送费 申请人撤回")
    @PostMapping("/ywPostageCancel")
    public CommonResult ywPostageCancel(@ApiParam(value = "数据主键") @RequestParam(required = true) String approveNo) {
        try {
            ywApproveService.ywPostageCancel(approveNo);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询审批表格item数据")
    @PostMapping("/getYwItemList")
    public CommonResult getYwItemList(@ApiParam(value = "数据主键") @RequestParam(required = true) String approveNo) {
        try {
            List<JSONObject> itemDataList = dknTrialItemService.getItemDataList(approveNo);
            return CommonResult.success(itemDataList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "排除数据")
    @PostMapping("/addExcludeData")
    public CommonResult addExcludeData(@RequestBody YwExcuteCreateDto dto) {
        try {
            dknTrialItemService.addExcludeData(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询排除数据")
    @PostMapping("/getExcludeDataList")
    public CommonResult getExcludeDataList(@RequestParam String approvalName) {
        try {
            List<JSONObject> excludeDataList = dknTrialItemService.getExcludeDataList(approvalName);
            return CommonResult.success(excludeDataList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "删除排除数据")
    @PostMapping("/removeExcludeData")
    public CommonResult removeExcludeData(String orderNo) {
        try {
            dknTrialItemService.removeExcludeData(orderNo);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    /**
     * batchSendYwXftQuery 和 batchSendYwXftSubmit 两者算法要保持一致
     * @param criteria0
     * @return
     */

    @ApiOperation(value = "推送薪福通前进行查询")
    @PostMapping("/batchSendYwXftQuery")
    public CommonResult batchSendYwXftQuery(DbApprovalLogQueryCriteria criteria0) {
        try {
            Integer day=criteria0.getDay();
            if(day==null){
                day=10;
            }
            String timeBefore = criteria0.getTimeBefore();
            if(UtilValidate.isEmpty(timeBefore)){
                return CommonResult.error("请选择时间节点!");
            }
            List<String> deptName=new ArrayList<>();
            deptName.add("OEM营销部");
            deptName.add("钟营销部");
         //   deptName.add("时俱佳营销部");
            deptName.add("钢表营销组");
            // 部门名称，部门编号
            Map<String,String> map=new LinkedHashMap<>();
            map.put("OEM营销部","HL02-2,HL02-3,HL08-0-7"); //迪卡侬,OEM营销部,东莞恒丽
            map.put("钟营销部","HL02-1"); //钟营销部
        //    map.put("时俱佳营销部","HL02-7"); //时俱佳营销部
            map.put("钢表营销组","HL02-4-1,HL04-2-5"); //钢表营销组  仓储物流部钢表组


            Date now=new Date();
            Map<String,List<ImportXftDataDto>> arr_list=new LinkedHashMap<>();

            String month =null;
            if(UtilValidate.isEmpty(criteria0.getMonth())){
                month =DateKit.dateFormat(now, "yyyy-MM")+"-01";
            }else{
                month =criteria0.getMonth();
            }
            //计算薪资属期
            String start=month+" 00:00:00";
            Date start_date = DateKit.dateFormat(start);//要结算的日期--关键 传入时默认是上个月的日期
            String setMonth=month.replaceAll("-01","");//设置薪资属期
         //

            String end = DateKit.getNextMonthStartDate(start);
            if("before".equals(timeBefore)){//10before,10after
                //当月10号前 -算到上个月
                end = DateKit.dateFormat(start_date, "yyyy-MM-"+day+" 23:59:59");
            }else if("after".equals(timeBefore)){
                //当月10号前 -算到下月
                start=DateKit.dateFormat(start_date, "yyyy-MM-"+(day+1)+" 00:00:00");
                //要结算的日期--关键
                String s = DateKit.dateFormat(start_date);
                String firstDayOfNextMonth = DateKit.getFirstDayOfNextMonth(s);//获取 下个月 1号 的日期
                setMonth=firstDayOfNextMonth.replaceAll("-01 00:00:00","").trim();
                end =DateKit.getFirstDayOfNextMonth(start);
            }
            //最终给薪福通的 日期格式 必须为 yyyyMM格式 例如 202507
            if(UtilValidate.isNotEmpty(setMonth)){
                //归属到上个月

                Date lastMonthDate = DateKit.getLastMonthBegin(DateKit.dateFormat(setMonth + "-01 00:00:00"));
//                setMonth=setMonth.replaceAll("-01 00:00:00","").trim();
                setMonth=DateKit.dateFormat(lastMonthDate,"yyyyMM");
            }

            System.out.println("---setMonth:"+setMonth);
            Set<Map.Entry<String, String>> entries = map.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                String depName = entry.getKey();
                String value = entry.getValue();

                List<String> salesmanDeptNos = Arrays.asList(value.split(","));

                YwMoneyAffirmLogCountQueryCriteria criteria=new YwMoneyAffirmLogCountQueryCriteria();
                criteria.setStartTime(start);
                criteria.setEndTime(end);

                criteria.setSalesmanDeptNoIn(salesmanDeptNos);
                List<CountMoneyUserDto> list = ywMoneyAffirmLogService.getCountDeptTotalLog_biao(criteria);
                List<ImportXftDataDto> data_list=new ArrayList<>();

                for (CountMoneyUserDto countMoneyUserDto : list) {
                    String userType = countMoneyUserDto.getUserType();
                    String userName = countMoneyUserDto.getUserName();
                    //钟部要结算经理抽成,其他不用


                    String passApproveNo=null;
                    if(UtilValidate.isNotEmpty(userName)){
                        DbApprovalLogQueryCriteria c2=new DbApprovalLogQueryCriteria();
                        c2.setUserName(userName);
                        c2.setStartTime(start);
                        c2.setEndTime(end);
                        passApproveNo = dbApprovalLogService.getPassApproveNoByUserName(c2);
                    }

                    ImportXftDataDto userDto=new ImportXftDataDto();
                    userDto.setUserType(userType);
                    userDto.setDeptName(depName);
                    userDto.setUserName(userName);
                    userDto.setUserPrice(countMoneyUserDto.getUserPrice());
                    userDto.setAddDate(setMonth);
                    if(!userType.equals("ji_jing")&&!userType.equals("manage_user")){
                        if(UtilValidate.isNotEmpty(passApproveNo)){
                            userDto.setApproveNo(passApproveNo);
                        }
                    }

                    data_list.add(userDto);
                }

                for (ImportXftDataDto importXftDataDto : data_list) {
                    importXftDataDto.setApproveType("业务员抽成审批");
                }

                if(UtilValidate.isNotEmpty(data_list)){
                    arr_list.put(depName,data_list);
                }

            }
            return CommonResult.success(arr_list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "推送薪福通前-确认推送")
    @PostMapping("/batchSendYwXftSubmit")
    public CommonResult batchSendYwXftSubmit(@RequestBody List<SubmitXftDataDto> arr_list) {
        try {
            if ("dev".equals(PD_MATCH)) {
                return CommonResult.error("开发环境不可推送！！");
            }
//            Date now=DateKit.getLastMonthBegin();
//            String month =DateKit.dateFormat(now, "yyyy-MM");
            List<ImportXftDataDto> data_list=new ArrayList<>();
            String month =arr_list.get(0).getAddDate();
            for (SubmitXftDataDto countMoneyUserDto : arr_list) {
                String userType = countMoneyUserDto.getUserType();
                String userName = countMoneyUserDto.getUserName();
                String deptName = countMoneyUserDto.getDeptName();
                //钟部要结算经理抽成,其他不用

                if("manage_user".equals(userType)&&"钟营销部".equals(deptName)){

                    countMoneyUserDto.setApproveNo(month+"钟部经理抽成");
                    ImportXftDataDto dto=new ImportXftDataDto();
                    BeanUtils.copyProperties(countMoneyUserDto,dto);
                    if(UtilValidate.isNotEmpty(countMoneyUserDto.getTotalPrice())){
                        dto.setUserPrice(countMoneyUserDto.getTotalPrice());
                    }
                    data_list.add(dto);
                }
                if("merchandiser_user".equals(userType)||"receiver_user".equals(userType)||"introduce_user".equals(userType)){

                    ImportXftDataDto dto=new ImportXftDataDto();
                    BeanUtils.copyProperties(countMoneyUserDto,dto);
                    if(UtilValidate.isNotEmpty(countMoneyUserDto.getTotalPrice())){
                        dto.setUserPrice(countMoneyUserDto.getTotalPrice());
                    }
                    data_list.add(dto);
                }
            }

            ImportJsonDataDto dto=new ImportJsonDataDto();
            dto.setList(data_list);
            dto.setType("业务员抽成审批");
            dto.setMonth(month);
            JSONArray objects = xftService.importJsonData(dto);
            if(UtilValidate.isNotEmpty(objects)){
                JSONObject jsonObject = objects.getJSONObject(0);
                if(jsonObject.containsKey("errorMessage")&&UtilValidate.isNotEmpty(jsonObject.getString("errorMessage"))){
                    return CommonResult.error(jsonObject.getString("errorMessage"));
                }
            }
            redisUtil.set("xft_push:"+month,month);
            try{
                String jsonString = JSONArray.toJSONString(arr_list);
                dbApprovalLogService.sendOemPriceToHhm(arr_list);
                redisUtil.set("xft_push:list:"+month,jsonString);
            }catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return CommonResult.success(data_list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "推送薪福通前-测试发送黄惠敏")
    @PostMapping("/sendYwXftTest")
    public CommonResult sendYwXftTest(@RequestBody List<SubmitXftDataDto> arr_list) {
        try {

            Date now=DateKit.getLastMonthBegin();
            String month =DateKit.dateFormat(now, "yyyy-MM");
            List<ImportXftDataDto> data_list=new ArrayList<>();

            for (SubmitXftDataDto countMoneyUserDto : arr_list) {
                String userType = countMoneyUserDto.getUserType();
                String userName = countMoneyUserDto.getUserName();
                String deptName = countMoneyUserDto.getDeptName();
                //钟部要结算经理抽成,其他不用

                if("manage_user".equals(userType)&&"钟营销部".equals(deptName)){

                    countMoneyUserDto.setApproveNo(month+"钟部经理抽成");
                    ImportXftDataDto dto=new ImportXftDataDto();
                    BeanUtils.copyProperties(countMoneyUserDto,dto);
                    if(UtilValidate.isNotEmpty(countMoneyUserDto.getTotalPrice())){
                        dto.setUserPrice(countMoneyUserDto.getTotalPrice());
                    }
                    data_list.add(dto);
                }
                if("merchandiser_user".equals(userType)||"receiver_user".equals(userType)||"introduce_user".equals(userType)){

                    ImportXftDataDto dto=new ImportXftDataDto();
                    BeanUtils.copyProperties(countMoneyUserDto,dto);
                    if(UtilValidate.isNotEmpty(countMoneyUserDto.getTotalPrice())){
                        dto.setUserPrice(countMoneyUserDto.getTotalPrice());
                    }
                    data_list.add(dto);
                }
            }

//            ImportJsonDataDto dto=new ImportJsonDataDto();
//            dto.setList(data_list);
//            dto.setType("业务员抽成审批");
//            xftService.importJsonData(dto);
//            redisUtil.set("xft_push:"+month,month);

            dbApprovalLogService.sendOemPriceToHhm(arr_list);

            return CommonResult.success(data_list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "查询推送按钮控制")
    @PostMapping("/batchSendYwXftBtn")
    public CommonResult batchSendYwXftBtn(DbApprovalLogQueryCriteria criteria0){
        try {
            String month =null;
            Date now=new Date();

            if(UtilValidate.isEmpty(criteria0.getMonth())){
                month = DateKit.dateFormat(now, "yyyy-MM");
            }else{
                String s = criteria0.getMonth();//2025-08-01
//                month = s.substring(0,s.length()-3); //这个控制就不要了，否则删redis还来找我
                month = s;
            }

            String v = redisUtil.getString("xft_push:" + month);
            return CommonResult.success(v);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询推送记录")
    @PostMapping("/batchSendYwXftLogs")
    public CommonResult batchSendYwXftLogs(DbApprovalLogQueryCriteria criteria0){
        try {

            Set<String> keys = redisUtil.keys("xft_push:list:*");
            Map<String,Object> map=new LinkedHashMap<>();
            for (String key : keys) {
                String string = redisUtil.getString(key);
                JSONArray jsonArray = JSONArray.parseArray(string);
                map.put(key,jsonArray);
            }
            return CommonResult.success(map);
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "考勤审批 发起前查看审批人")
    @PostMapping("/kqApprovalCreateView")
    public CommonResult kqApprovalCreateView() {
        try {
            // "业务员提成审批"
            ApprovalCreateViewDto dto = kqDakaLogService.kqApprovalCreateView();
            return CommonResult.success(dto);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "车间考勤 审批提起")
    @PostMapping("/chejianKqSubmit")
    public CommonResult chejianKqSubmit(@RequestBody KqCreateDto dto) {
        try {

            ApproveRespond approveRespond = ywApproveService.chejianKqSubmit(dto);
            return CommonResult.success(approveRespond);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "通用 审批通过")
    @PostMapping("/chejianKqPass")
    public CommonResult chejianKqPass(YwApprovaPassDto dto) {
        try {
            ywApproveService.chejianKqPass(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "车间考勤 审批驳回")
    @PostMapping("/chejianKqBack")
    public CommonResult chejianKqBack(YwApprovaPassDto dto) {
        try {
            ywApproveService.chejianKqBack(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "车间考勤 提起人撤回")
    @PostMapping("/chejianKqCancel")
    public CommonResult chejianKqCancel(YwApprovaPassDto dto) {
        try {
            ywApproveService.chejianKqCancel(dto);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "通用 审批详情")
    @PostMapping("/approvalView")
    public CommonResult approvalView(String approveNo) {
        try {
            ApprovalCreateViewDto dto = ywApproveService.commonView(approveNo);
            return CommonResult.success(dto);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "通用 审批列表")
    @PostMapping("/approvalList")
    public CommonResult approvalList(DbApprovalLogQueryCriteria criteria, YmPageDto ymPage) {
        try {
            String userName = getUserName();
            String userRole = getUserRole();
            String userId = getUserId();

            boolean is_show_all=false;//是否拥有查看所有数据的权限
            if ("manage".equals(userRole)) {
                is_show_all=true;
            }
            if( userName.equals("戴燕云")||userName.equals("曾旻銮")||userName.equals("庄嫚艺")){
                is_show_all=true;
            }
            if (!is_show_all) {
                criteria.setAllUserIds(userId);
            }

            TableList<DbApprovalLog> list = dbApprovalLogService.list(criteria, ymPage);
            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }





}
